package com.uchedao.ulibrary.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.Toast;

import com.uchedao.ulibrary.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Activity 父类
 * @author zhengxizhen 2015-10-13
 * */
public abstract class BaseFragmentActivity extends FragmentActivity implements
		CommonActivity {
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.act_fragment_frame);
		initFragment();
	}

	public final void toFragment(Fragment fragment, boolean canBack) {
		toFragment(fragment, canBack, false);
	}

	public void postToRightAnim(FragmentTransaction mTran) {
		mTran.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
	}

	public void postToLeftAnim(FragmentTransaction mTran) {
		mTran.setCustomAnimations(R.anim.push_right_in, R.anim.push_right_out);
	}

	@Override
	public void toFragment(Fragment fragment, boolean canBack, boolean anim) {
		FragmentMrg.toFragment(this, fragment, canBack, anim);
	}

	private int exitCount = 0;

	private void eixt() {
		this.finish();
	}

	public void tryExit() {
		if (exitCount++ > 0) {
			eixt();
		} else {
			Toast.makeText(this, "再按一次“返回”退出", Toast.LENGTH_SHORT).show();
			Timer timer = new Timer();
			timer.schedule(new TimerTask() {
				@Override
				public void run() {
					exitCount = 0;
				}
			}, 1000);
		}

	}

	@Override
	public void onBackPressed() {
		if (!FragmentMrg.onKeyBack(this)) {
			this.finish();
			super.onBackPressed();
		}
	}
	/**
	 * 展示消息
	 * */
	public void showToast(String strMsg) {
		Toast.makeText(this, strMsg, Toast.LENGTH_LONG).show();
	}

	public abstract void initFragment();

	public abstract boolean isProgressDialogShowing();

	public abstract void showProgressDialog(String msg);

	public abstract void cancelProgressDialog();

}
